﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility; 

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class InstrumentMaintainExtra : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["RegisterID"] != null)
                {
                    hiddenRegisterID.Value = Request.QueryString["RegisterID"];
                    hiddenMaintainID.Value = Request.QueryString["MaintainID"];

                    BindGrid();
                    btnCheck.Enabled = false;

                    MaintainBillBll bll = new MaintainBillBll();
                    WsmMaintainBillEntity entity = bll.Get<WsmMaintainBillEntity>(new Guid(hiddenMaintainID.Value));
                    if (entity != null)
                    {
                        AccessoryReplaceMemo.Text = entity.AccessoryReplaceMemo;
                        if (entity.AccessoryCheckStatus == 1)
                        {
                            hiddenStatus.Value = "1";
                            btnSubmit.Enabled = false;
                            if (base.CheckUserHasFuction(1, "Func5015"))
                            {
                                btnCheck.Enabled = true;
                            }
                        }

                        if (entity.AccessoryCheckStatus > 1)
                        {
                            hiddenStatus.Value = entity.AccessoryCheckStatus.ToString();
                            btnSubmit.Enabled = false;
                            btnCheck.Enabled = false;
                            AccessoryReplaceMemo.Enabled = false;
                        }
                    }
                }
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        { 
            StockBll bll = new StockBll();
            DataTable dt = bll.GetAccessoryReplacement(" and MaintainID='"+hiddenMaintainID.Value+"'");
            if (dt != null && dt.Rows.Count > 0)
            {
                hiddenHasAccessory.Value = "1";
            }
            else
                hiddenHasAccessory.Value = "0";

            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind();
        }

        protected void gridAccessoryList_DeleteCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            Guid ReplacementID = new Guid(gridAccessoryList.MasterTableView.DataKeyValues[e.Item.ItemIndex]["ReplacementID"].ToString());
            StockBll bll = new StockBll();

            WsmAccessoryReplacementEntity entity = new SystemBll().Get<WsmAccessoryReplacementEntity>(ReplacementID);
            WsmAccessoryStockEntity stockEntity = new SystemBll().Get<WsmAccessoryStockEntity>(entity.StockID);
            if (stockEntity.AccessoryStatus == 2)
            {
                stockEntity.AccessoryStatus = 1;
            }
            if (stockEntity.AccessoryStatus == 3)
            {
                stockEntity.AccessoryStatus = 0;
            }
            new SystemBll().Update(stockEntity);
            bll.Delete(new WsmAccessoryReplacementEntity(ReplacementID));
            BindGrid();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            MaintainBillBll bll = new MaintainBillBll();
            bll.UpdateAccessoryCheckStatus(Enumerator.AccessoryReplacementCheckStatus.Submit, new Guid(hiddenMaintainID.Value),base.CurrentUserID,base.CurrentUserEntity.UserName.ToString(),"");
            btnCheck.Enabled = false;
            hiddenStatus.Value = "1";

            BindGrid();
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            MaintainBillBll bll = new MaintainBillBll();
            bll.UpdateAccessoryCheckStatus(Enumerator.AccessoryReplacementCheckStatus.Checked, new Guid(hiddenMaintainID.Value), base.CurrentUserID, base.CurrentUserEntity.UserName.ToString(), AccessoryReplaceMemo.Text);
            btnCheck.Enabled = false;
            hiddenStatus.Value = "1";

            BindGrid();
        }


        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                Label lblAccessoryPaymentStatus = e.Item.FindControl("lblAccessoryPaymentStatus") as Label;
                Label lblBelongto = e.Item.FindControl("lblBelongto") as Label;
                Label lblCheckStatus = e.Item.FindControl("lblCheckStatus") as Label;
                LinkButton lnkbtnDelete = e.Item.FindControl("lnkbtnDelete") as LinkButton;
                DataRowView dv = e.Item.DataItem as DataRowView;

                if (dv != null)
                {
                    lblAccessoryPaymentStatus.Text = dv["AccessoryPaymentStatus"].ToString() == "0" ? "公司付":"客户付";
                    lblBelongto.Text = EnumeratorHelper.GetFieldText(((Enumerator.AccessoryReplacementBelongto)Convert.ToInt32(dv["ReplacementBelongto"].ToString())));
                    
                    if (Convert.ToInt32(dv["CheckStatus"].ToString()) > 0)
                    {
                        lnkbtnDelete.Enabled = false;
                    }

                    lblCheckStatus.Text = EnumeratorHelper.GetFieldText(((Enumerator.AccessoryReplacementCheckStatus)Convert.ToInt32(dv["CheckStatus"].ToString())));

                
                }
            }
        }
    }
}
